package com.example.hans.internetandroid;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity implements View.OnClickListener{

    private Button mButtonToWebView;
    private Button mButtonToHttpTest;
    private Button mButtonToOkHttpTest;
    private Button mButtonToTestActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonToWebView = (Button)findViewById(R.id.btn_toWebView);
        mButtonToWebView.setOnClickListener(this);

        mButtonToHttpTest = (Button)findViewById(R.id.btn_toHttpTest);
        mButtonToHttpTest.setOnClickListener(this);

        mButtonToOkHttpTest = (Button)findViewById(R.id.btn_toOkHttpTest);
        mButtonToOkHttpTest.setOnClickListener(this);

        mButtonToTestActivity = (Button)findViewById(R.id.btn_toTestActivity);
        mButtonToTestActivity.setOnClickListener(this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_toWebView: {
                Intent intent = new Intent(MainActivity.this,WebViewTest.class);
                startActivity(intent);
                break;
            }
            case R.id.btn_toHttpTest: {
                Intent intent = new Intent(MainActivity.this,HttpTest.class);
                startActivity(intent);
                break;
            }
            case R.id.btn_toOkHttpTest: {
                Intent intent = new Intent(MainActivity.this,OkHttpTest.class);
                startActivity(intent);
                break;
            }
            case R.id.btn_toTestActivity: {
                Intent intent = new Intent("InternetAndroid.TestActivity");
                startActivity(intent);
                break;
            }
            default:
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK: {
                // 监控返回键
                new AlertDialog.Builder(MainActivity.this).setTitle("提示")
                        .setIconAttribute(android.R.attr.alertDialogIcon)
                        .setMessage("确定要退出吗?")
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MainActivity.this.finish();
                            }})
                        .setNegativeButton("取消", null)
                        .create().show();
            }
        }

        return super.onKeyDown(keyCode, event);
    }
}
